package com.fram.service.admin.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.admin.circle.request.ResourceCircleRequest;
import com.fram.bean.admin.circle.response.ResourceCircleResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.ResourceCircle;
import com.fram.mapper.ResourceCircleMapper;
import com.fram.service.admin.CircleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CircleServiceImpl implements CircleService {
	@Autowired
	private ResourceCircleMapper resourceCircleMapper;

	@Override
	public PageInfo<ResourceCircleResponse> list(PageBean<ResourceCircleRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<ResourceCircleResponse> list = resourceCircleMapper.selectAafterList(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void update(ResourceCircle request) throws ProAffairException {
		if (StringUtils.isEmpty(request.getId())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		if (resourceCircleMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

}
